 //
//  Plant+CRUD.m
//  PlotMate
//
//  Created by Brian Boyle on 26/03/2012.
//  Copyright (c) 2012 Brian Boyle. All rights reserved.
//

#import "Plant+CRUD.h"

@implementation Plant (CRUD)

/*Create a Plant from a CSV file's contents. Used for creating plants in dev mode*/
+(Plant *)createPlantWithCSVContent:(NSArray *)csvRow inContext:(NSManagedObjectContext *)context
{
    
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Plant"];
    request.predicate = [NSPredicate predicateWithFormat:@"name = %@", [csvRow objectAtIndex:0]];
    
    NSError *error;
    NSArray *existingPlants = [context executeFetchRequest:request error:&error];
    Plant *plant;

    if([existingPlants count] == 0){
    plant = [NSEntityDescription insertNewObjectForEntityForName:@"Plant" inManagedObjectContext:context];
    plant.name = [csvRow objectAtIndex:0];
    plant.desc = [csvRow objectAtIndex:1];
    plant.soil = [csvRow objectAtIndex:2];
    plant.position = [csvRow objectAtIndex:3];
    plant.category = [csvRow objectAtIndex:4];
    plant.sowMonths = [csvRow objectAtIndex:5];
    plant.harvestMonths = [csvRow objectAtIndex:6];
    plant.season = [csvRow objectAtIndex:7];
    } else {
        plant = [existingPlants lastObject];
    }
    return plant;
}

/*Create a Companion Plant from a CSV file's contents. Used for creating companions in dev mode*/
+(Plant *)createCompanionsWithCSVContent:(NSArray *)csvRow inContext:(NSManagedObjectContext *)context
{
    //Get the plant we are adding companions for
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Plant"];
    request.predicate = [NSPredicate predicateWithFormat:@"name = %@", [csvRow objectAtIndex:0]];
    
    NSError *error;
    NSArray *plantList = [context executeFetchRequest:request error:&error];
    Plant *plant = [plantList objectAtIndex:0];
    
    NSLog(@"Creating Companions for Plant == %@", plant.name);
    [[plant mutableSetValueForKey:@"companions"] removeAllObjects];
    
    for (int i = 1; i < [csvRow count]; i++) {
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Plant"];
        NSString *companionString = [csvRow objectAtIndex:i];
        request.predicate = [NSPredicate predicateWithFormat:@"name = %@", [companionString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
        
        NSError *error;
        NSArray *plants = [context executeFetchRequest:request error:&error];
        Plant *companionPlant = [plants objectAtIndex:0];
        NSLog(@"Adding Companion %@", companionPlant.name);
        [plant addCompanionsObject:companionPlant];
    }
    NSLog(@"THe plant has now got companions %d", [plant.companions count]);
    return plant;
}

/*Create a Incompatible Plant from a CSV file's contents. Used for creating incompatible plants in dev mode*/
+(Plant *)createIncompatiblesWithCSVContent:(NSArray *)csvRow inContext:(NSManagedObjectContext *)context
{
    //Get the plant we are adding companions for
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Plant"];
    request.predicate = [NSPredicate predicateWithFormat:@"name = %@", [csvRow objectAtIndex:0]];
    
    NSError *error;
    NSArray *plantList = [context executeFetchRequest:request error:&error];
    Plant *plant = [plantList objectAtIndex:0];
    
    NSLog(@"Creating Incompatibles for Plant ** %@", plant.name);
    [[plant mutableSetValueForKey:@"incompatibles"] removeAllObjects];
    
    for (int i = 1; i < [csvRow count]; i++) {
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Plant"];
        NSString *companionString = [csvRow objectAtIndex:i];
        request.predicate = [NSPredicate predicateWithFormat:@"name = %@", [companionString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
        
        NSError *error;
        NSArray *plants = [context executeFetchRequest:request error:&error];
        Plant *incompatiblePlant = [plants objectAtIndex:0];
        NSLog(@"Adding Incompatible %@", incompatiblePlant.name);
        [plant addIncompatiblesObject:incompatiblePlant];
    }
    NSLog(@"The plant %@ has now got %d incompatibles", plant.name, [plant.incompatibles count]);
    return plant;
}

+(NSArray *)loadAllPlantsInContext:(NSManagedObjectContext *)context
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Plant"];
    NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    request.sortDescriptors = [NSArray arrayWithObject:sorter];
    
    NSError *error;
    NSArray *plantList = [context executeFetchRequest:request error:&error];
    return plantList;
}

+(NSArray *)loadAllPlantsInArray:(NSArray *)plantNames withContext:(NSManagedObjectContext *)context
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Plant"];
    request.predicate = [NSPredicate predicateWithFormat:@"name in %@", plantNames];
    NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    request.sortDescriptors = [NSArray arrayWithObject:sorter];
    
    NSError *error;
    NSArray *plantList = [context executeFetchRequest:request error:&error];
    return plantList;
}

@end
